local leiji = fk.CreateSkill{
  name = "klmou__leiji",
}

Fk:loadTranslationTable{
  ["klmou__leiji"] = "雷击",
  [":klmou__leiji"] = "游戏开始时，你对【杀】【闪】【桃】【酒】进行排列组合，当你按顺序使用或打出这些基本牌时，你可以选择一名其他角色进行判定："..
  "<br>若为黑色，你对其造成两点雷电伤害；<br>若为红色，你摸两张牌或回复一点体力。",

  ["#klmou__leiji-start"] = "雷击：按顺序排列【杀】【闪】【桃】【酒】，当你按顺序使用或打出这些牌时，你可以令一名其他角色判定并执行相应效果",
  ["#klmou__leiji-choose"] = "雷击：令一名其他角色进行判定，若为黑色，你对其造成2点雷电伤害；若为红色，你摸两张牌或回复一点体力",
  ["@klmou__leiji"] = "雷击",
 
  ["klmou__slash"] = "<font color=\"#E0DB2F\">杀</font>",
  ["klmou__jink"] = "<font color=\"#E0DB2F\">闪</font>",
  ["klmou__peach"] = "<font color=\"#E0DB2F\">桃</font>",
  ["klmou__analeptic"] = "<font color=\"#E0DB2F\">酒</font>",

  ["$klmou__leiji1"] = "乱戚逆党，怎敢犯天公之威！",
  ["$klmou__leiji2"] = "雷霆之威，可破万钧之势！",
}

leiji:addEffect(fk.GameStart, {
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(leiji.name)
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local choices = {"slash", "jink", "peach", "analeptic"}
    local sorted = {}
    local result = player.room:askToChoices(player, {
      choices = choices,
      min_num = 3,
      max_num = 4,
      prompt = "#klmou__leiji-start",
      skill_name = leiji.name,
      cancelable = false,
    })
    for i, v in ipairs(result) do
      sorted[i] = Fk:translate(v)
    end
    if #sorted ~= 4 then
      local one = table.filter(choices, function(v) return not table.contains(result, v) end)
      if #one > 0 then
        table.insert(sorted, Fk:translate(one[1]))
      end
    end
    player.room:setPlayerMark(player, "@klmou__leiji", sorted)
  end,
})

local spec = {
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(leiji.name) and target == player and data.card then
      local mark = player:getTableMark("@klmou__leiji")
      for i, _ in ipairs(mark) do
        if mark[i] == Fk:translate(data.card.trueName) then
          if i == 1 then
            if not mark[i]:startsWith("<font") then
              event:setCostData(self, { num = i })
              return true
            end
          else
            if not mark[i]:startsWith("<font") and mark[i - 1]:startsWith("<font") then
              event:setCostData(self, { num = i })
              return true
            end
          end
        end
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local num = event:getCostData(self).num
    local to = room:askToChoosePlayers(player, {
      min_num = 1,
      max_num = 1,
      targets = room:getOtherPlayers(player),
      skill_name = leiji.name,
      prompt = "#klmou__leiji-choose",
      cancelable = true,
    })
    if #to > 0 then
      event:setCostData(self, {tos = to , n = num })
      return true
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local to = event:getCostData(self).tos[1]
    local num = event:getCostData(self).n
    local mark = player:getTableMark("@klmou__leiji")
    mark[num] = Fk:translate("klmou__"..data.card.trueName)
    if table.every(mark, function(v) return v:startsWith("<font") end) then
      for i, v in ipairs(mark) do
        mark[i] = mark[i]:match('>(.-)<')
      end
    end
    room:setPlayerMark(player, "@klmou__leiji", mark)
    local judge = {
      who = to,
      reason = leiji.name,
      pattern = ".",
    }
    room:judge(judge)
    if judge.card.color == Card.Black then
      if not to.dead then
        room:damage{
          from = player,
          to = to,
          damage = 2,
          damageType = fk.ThunderDamage,
          skillName = leiji.name,
        }
      end
    elseif judge.card.color == Card.Red then
      if player.dead then return end
      local ch = room:askToChoice(player, {
        choices = {"draw2", "recover"},
        skill_name = leiji.name,
      })
      if ch == "draw2" then
        player:drawCards(2, leiji.name)
      elseif ch == "recover" then
        if player:isWounded() then
          room:recover{ who = player, num = 1, recoverBy = player, skillName = leiji.name }
        end
      end
    end
  end,
}

leiji:addEffect(fk.CardUsing, spec)
leiji:addEffect(fk.CardResponding, spec)


return leiji
